<?php 
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use cmf\controller\BaseController;

use think\Db;

	class WxreceivetypeController extends AdminBaseController
	{



		function set_receive(){
			// if($_)
			 //var_dump($_POST);
			$id=session('ADMIN_ID');
			if($_POST){
				if(!empty($_POST['site_types'])){
					$data['receive_site'] = implode(',', $_POST['site_types']);
				}else{
					$data['receive_site'] = '';
				}
				if(!empty($_POST['receive_user'])){
					$data['receive_user'] = $_POST['receive_user'];
				}else{
					$data['receive_user'] = 1;
				}
				
				$update = db('user')->where('id',$id)->update($data);
				//echo db()->getLastsql();
				if($update){
					$this->success('设置成功','set_receive');
				}else{
					$this->error('设置失败');
				}
			}
			$info = db('user')->where('id',$id)->field('receive_site,receive_user')->find();
			$site_types = $this->site_type();
			$this->assign('receive_user',$info['receive_user']);
			$this->assign('receive_site',explode(',',$info['receive_site']));
			//var_dump($site_types);
		//	$site_types = json_decode(json_encode($site_types),true);
			//var_dump($site_types);
			$this->assign('site_types',$site_types);
			return $this->fetch();


		}


		function site_type(){
			// echo config('img_host');
			
			// exit;

			$where=array('t.name'=>'场地类型');
			$list=db("category_type")->alias('t')->join('category c','t.id=c.type_id')->field('c.id,c.name,c.img')->where($where)->select();
			$list=json_decode(json_encode($list),true);
			// foreach ($list as $key => $value) {
			// 	$condition=' site_type_id = "'.$value['id'].'" ';
			// 	$site_info=db('site')->where($condition)->find();
			// 	if(empty($site_info)){
			// 		unset($list[$key]);
			// 	}
			// }
			foreach ($list as $key => $value) {
				if($value['img']){
					$list[$key]['img']=config('img_host').$value['img'];
				}
				$condition=' site_type_id = "'.$value['id'].'" ';
				$site_info=db('site')->where($condition)->find();
				if(empty($site_info)){
					unset($list[$key]);
				}
				
			}

			return $list;
		}





























	}
